package net

import (
	"net"
	"gitee.com/vzr/gearutil/log"
	"net/http"
)

var (
	client *http.Client
)

// GetHostIP 获取本机地址
func GetHostIP() ([]string, error) {
	var ips []string
	addrs, err := net.InterfaceAddrs()

	if err != nil {
		log.Err.Printf("Get Host IP errors:%s\n", err)
	}

	for _, addr := range addrs {
		// 检查ip地址判断是否回环地址
		if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.IsGlobalUnicast() {
			if ipnet.IP.To4() != nil {
				ips = append(ips, ipnet.IP.String())
			}
		}
	}
	return ips, nil
}

// HttpGet 请求Get方法
func HttpGet(request *http.Request) *http.Response {
	if client == nil {
		client = &http.Client{}
	}
	resp, err := client.Do(request)
	if err != nil {
		log.Err.Println(err)
	}
	return resp
}
